﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using UIWindows.Commands;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Models.Homes;
using UIWindows.Tools.Helper;
using UIWindows.ViewModels.Dialogs;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels.HomeItemViews
{
    /// <summary>
    /// 后段主页ViewModel
    /// </summary>
    public class AfertViewModel : UIWindows.Domains.ViewModelBase, XingLucifer.IBase.IViewStatus
    {
        #region 配置显示项
        public GridView PlateDataView { get; set; }

        public GridView CleaningDataView { get; set; }
        public GridView MantleDataView { get; set; }
        public GridView MarkingDataView { get; set; }


        #endregion
        public GlobalStatics.ParameterStatic Parameter { get; set; }
        public GlobalStatics.HomeStatic Home { get; set; }
        public GlobalStatics.TemporaryStatic _temporary { get; set; }

        public DeviceInteractiveaddressStatic DeviceInteractiveaddressData { get; set; }
        public SystemConfigStatic _configStatic { get; set; }
        public DisplayItemsStatic _displayItems { get; set; }


        public AfertViewModel(GlobalStatics.ParameterStatic parameter, GlobalStatics.HomeStatic home, GlobalStatics.TemporaryStatic temporaryStatic, SystemConfigStatic configStatic, DisplayItemsStatic displayItems, DeviceInteractiveaddressStatic deviceInteractiveaddressStatic)
        {
            DeviceInteractiveaddressData = deviceInteractiveaddressStatic;
            Parameter = parameter;

            _temporary = temporaryStatic;
            //parameter.MESCollectionItemsParameter[MachineInfoTypes.卷芯上料].MachineStatus = "0";
            Home = home;
            PlateDataView = new GridView();
            CleaningDataView = new GridView();
            MantleDataView = new GridView();
            MarkingDataView = new GridView();


        }


        public DelegateCommand ClockOutline => new DelegateCommand(() =>
        {

        });

        //private List<DisplayItemsModel> _displayItems_ShortCircuit;
        //private List<DisplayItemsModel> _displayItems_BeforeWeight;

        public void Load()
        {
            if (_displayItems == null)
            {
                _displayItems = Domains.ObjectContainer.ResolveSingleton<DisplayItemsStatic>();
            }
            if (!_displayItems.IsHomeUpdate)
            {
                return;
            }
            PlateDataView.Columns.Clear();
            CleaningDataView.Columns.Clear();
            MantleDataView.Columns.Clear();
            MarkingDataView.Columns.Clear();
            UpdateProductionDataViewColumns(PlateDataView, _displayItems.PlateDataDisplayItems);
            UpdateProductionDataViewColumns(CleaningDataView, _displayItems.CleaningDataDisplayItems);
            UpdateProductionDataViewColumns(MantleDataView, _displayItems.MantleDataDisplayItems);
            UpdateProductionDataViewColumns(MarkingDataView, _displayItems.MarkingDataDisplayItems);

            _displayItems.IsHomeUpdate = false;

            //var testData = new CupSleeveChangingData
            //{
            //    AcquisitionTime = 12346,
            //    Operator="ning"
            //};
            //Home.AddBattery(testData, MachineInfoTypes.换套杯);

        }

        private void UpdateProductionDataViewColumns(GridView View, ObservableCollection<DisplayItemsModel> Items)
        {

            if (!_displayItems.IsHomeUpdate)
            {
                return;
            }

            View.Columns.Clear();

            List<DisplayItemsModel> list = new List<DisplayItemsModel>(Items);
            list.Sort((x, y) => x.CompareTo(y));

            foreach (var item in list)
            {
                if (item.IsDisplay)
                {
                    GridViewColumn gvc = new GridViewColumn { Header = item.Description };

                   if(item.Description.Contains("托盘码"))
                    {
                        gvc.Width =180;
                    }

                    if (item.DataType == "结果")
                    {
                        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
                        border.SetBinding(Border.BackgroundProperty, new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ResultToColorConverter()
                        });

                        FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                        text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
                        text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
                        text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

                        border.AppendChild(text);
                        var dataTemplate = new DataTemplate();
                        dataTemplate.VisualTree = border;
                        gvc.CellTemplate = dataTemplate;
                    }
                    else if (item.DataType == "浮点型")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "F2"
                        };
                    }
                    else if (item.DataType == "时间型")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "yyyy-MM-dd HH:mm:ss",
                            Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
                        };
                    }
                    else if (item.DataType == "扫码位置")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ScanPositionTypeConverter()
                        };
                    }
                    else if (item.DataType == "设备状态")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.MachineStatusConverter()
                        };
                    }
                    else
                    {
                        gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };
                    }

                    View.Columns.Add(gvc);
                }
            }
        }


        public void UnLoad()
        {

        }
        public DelegateCommand CmdMesView => new DelegateCommand(() =>
        {
            // 创建窗口的实例
            UIWindows.Views.Dialogs.MesInfoView mesInfoView = new UIWindows.Views.Dialogs.MesInfoView();

            mesInfoView.DataContext = new MesInfoViewModel(Parameter, mesInfoView);

            // 设置窗口在屏幕中间显示
            mesInfoView.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            // 显示窗口
            mesInfoView.ShowDialog();
        });


        public DelegateCommand ClickMe => new DelegateCommand(() =>
        {
            MySqlHelper mySqlHelper = new MySqlHelper(Parameter.MESParameter.OthenDatabaseurl);
            var oneData = mySqlHelper.GetBatteryByCode("Empty83564123456789024720");
            if (oneData.status)
            {
                var NetWeight = oneData.netWeight;
                var OneAfterWeighing = oneData.outputWeight;
            }
        });
   
        public DelegateCommand TestCMD => new DelegateCommand(() =>
        {
            int b = 239;
            double c = (double)b / 240;
            double a = Math.Round(c, 4, MidpointRounding.ToNegativeInfinity);
            a = a * 100;
            $"{a}".MessageDialogs(MessageLevelType.信息, false, Enums.PopUpTypes.Global);
        });
    }
}
